/*
 * COAPI - Copyright (C) 2019-2020 Cocheer Tech. Co., Ltd.
 * All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <smart_audio.h>
#include "coapi_cmd.h"
#include "coapi_timer.h"
#include "coapi_bindcode.h"
#include "coapi_app.h"
#include "coapi_logger.h"

#include <smart_audio.h>

/**
 * @brief 开关机相关的语音控制指令
 */
void coapi_cmd_type_switch(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_SWITCH_BOOTUP:    /* 开机*/
            break;
        case CMD_SWITCH_SHUTDOWN:  /* 关机 */
            break;
        case CMD_SWITCH_REBOOT:    /* 重启 */
            break;
        default:
            break;
    }
}

/**
 * @brief 待机唤醒相关的语音控制指令
 */
void coapi_cmd_type_standby(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch(cmd) {
       case CMD_STANDBY_ON:       /* 休眠 */
            break;
        case CMD_STANDBY_WAKEUP:  /* 唤醒 */
            break;
        default:
			break;
    }
}

/**
 * @brief 音量相关的语音控制指令
 */
void coapi_cmd_type_volume(int action)
{
	/* 获取音量值 */
    int val = 0;
    int cmd = 0;
	
	cmd = coapi_get_ctrl_cmd(action);	
	val = coapi_get_ctrl_val(action);

    switch (cmd) {
        case CMD_VOLUME_UP: /* 音量加 */
            smtaudio_vol_up(10);
            break;
        case CMD_VOLUME_DOWN: /* 音量减 */
            smtaudio_vol_down(10);
            break;
        case CMD_VOLUME_VAL: /* 设置音量值 */
            smtaudio_vol_set(val);
            break;
        case CMD_VOLUME_MUTE: /* 静音 */
            smtaudio_pause();
            break;
        case CMD_VOLUME_MAX: /* 最大音量 */
            smtaudio_vol_set(100);
            break;
        case CMD_VOLUME_MIN: /* 最小音量 */
            smtaudio_vol_set(10);
            break;
        case CMD_VOLUME_MUTE_CANCEL: /* 取消静音 */
            smtaudio_resume();
            break;
        default:
            break;
    }
}

/**
 * @brief 灯相关的语音控制指令
 */
void coapi_cmd_type_led(int action)
{
    int cmd = 0;
	/* 获取亮度值 */
    int val = 0;
	
	cmd = coapi_get_ctrl_cmd(action);
    val = coapi_get_ctrl_val(action);

    switch (cmd) {
        case CMD_LED_ON:  /* 开灯 */
            break;
        case CMD_LED_OFF:  /* 关灯 */
            break;
        case CMD_LED_UP:  /* 亮度调高 */
            break;
        case CMD_LED_DOWN:  /* 亮度调低 */
            break;
        case CMD_LED_BLUE:  /* 蓝灯 */
            break;
        case CMD_LED_GREEN:  /*  绿灯 */
            break;
        case CMD_LED_VIOLET:  /* 紫灯 */
            break;
        case CMD_LED_WHITE:  /* 白灯 */
            break;
        case CMD_LED_WARM:  /* 暖光灯 */
            break;
        case CMD_LED_COLD:  /* 冷光灯 */
            break;
        case CMD_LED_SUNLIGHT:  /* 日光灯 */
            break;
        case CMD_LED_MOONLIGHT:  /* 月光灯 */
            break;
        case CMD_LED_BREATING:  /* 呼吸灯 */
            break;
        case CMD_LED_BREATING_UP:  /* 加速呼吸 */
            break;
        case CMD_LED_BREATING_DOWN:  /* 减速呼吸 */
            break;
        case CMD_LED_COLORFUL:  /* 七彩灯 */
            break;
        case CMD_LED_BRIGHTNESS_VAL:  /* 亮度值 */
            break;
        case CMD_LED_BRIGHTNESS_MAX:  /* 亮度最大 */
            break;
        case CMD_LED_BRIGHTNESS_MIN:  /* 亮度最低 */
            break;
		case CMD_LED_ORANGE:			//橙灯
		case CMD_LED_CANDLELIGHT:	//烛光灯
		case CMD_LED_RHYTHM:			//彩灯律动
		case CMD_LED_NATURE:			//自然灯
		case CMD_LED_TUNE_WARM:		//调暖
		case CMD_LED_TUNE_COLD:		//调冷
		case CMD_LED_SLEEP_MODE:		//睡眠模式
		case CMD_LED_READ_MODE:		//阅读模式
        default:
            break;
    }
}

/**
 * @brief 运动相关的语音控制指令
 */
void coapi_cmd_type_battery(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_BATTERY_CHECK:  /* 电量检查 */
            break;
        default:
            break;
    }
}

/**
 * @brief 运动相关的控制指令
 */
void coapi_cmd_type_motion(int action)
{
	/* 速度值 */
    int val = 0;
    int cmd = 0;
	
	cmd = coapi_get_ctrl_cmd(action);
	val = coapi_get_ctrl_val(action);

    switch (cmd) {
        case CMD_MOTION_ON:  /* 开始运动 */
            break;
        case CMD_MOTION_OFF:  /* 停止运动 */
            break;
        case CMD_MOTION_SPEED_UP:  /* 提高速度 */
            break;
        case CMD_MOTION_SPEED_DOWN:  /* 降低速度 */
            break;
        case CMD_MOTION_SPEED_HIGH:  /* 高速 */
            break;
        case CMD_MOTION_SPEED_LOW:  /* 低速*/
            break;
        case CMD_MOTION_SPEED_MID:  /* 中速 */
            break;
        case CMD_MOTION_DIREC_LEFT:  /* 向左转 */
            break;
        case CMD_MOTION_DIREC_RIGHT:  /* 向右转 */
            break;
        case CMD_MOTION_DIREC_FORWARD:  /* 向前 */
            break;
        case CMD_MOTION_DIREC_BACKWARD:  /* 向后 */
            break;
        case CMD_MOTION_SPEED_VAL:  /* 控制速度值 */
            break;
        case CMD_MOTION_SPEED_MAX:  /* 设置最高速度 */
            break;
        case CMD_MOTION_SPEED_MIN:  /* 设置最低速度 */
			break;
		case CMD_MOTION_STAND_AT_ATTENTION: /* 立正 */
        case CMD_MOTION_STAND_AT_EASE: /* 稍息 */
        case CMD_MOTION_DIREC_LEFT_GO_N: /* 往左走N步 */
        case CMD_MOTION_DIREC_RIGHT_GO_N: /* 往右走N步 */
        case CMD_MOTION_DIREC_FORWARD_GO_N: /* 往前走N步 */
        case CMD_MOTION_DIREC_BACKWARD_GO_N: /* 往后走N步 */
        case CMD_MOTION_GO_N: /* 走N步 */
        case CMD_MOTION_TRANSFORM: /*变形*/
               break;
            break;
        default:
            break;
    }
}

/**
 * @brief 闹钟铃声相关的控制指令
 */
void coapi_cmd_type_ring(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_RING_START:  /* 开启铃声 */
            break;
        case CMD_RING_STOP:   /* 关闭铃声 */
            break;
        default:
            break;
    }
}

/**
 * @brief 播放相关的语音控制指令处理
 */
void coapi_cmd_type_player(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_PLAYER_START: /* 开始播放 */
            break;
        case CMD_PLAYER_STOP: /* 停止播放 */
            break;
        case CMD_PLAYER_PAUSE: /* 暂停播放 */
            xinfo("will pause player");
            smtaudio_pause();
            break;
        case CMD_PLAYER_CONTINUE: /* 继续播放 */
            xinfo("will resume player");
            smtaudio_resume();
            break;
        case CMD_PLAYER_PREV: /* 上一曲 */
            break;
        case CMD_PLAYER_NEXT: /* 下一曲 */
            break;
        case CMD_PLAYER_RANDOM: /* 随机播放 */
            break;
        case CMD_PLAYER_SINGLE: /* 单曲循环播放 */
            break;
        case CMD_PLAYER_LOOP: /* 列表循环播放 */
            break;
        case CMD_PLAYER_LOCAL: /* 本地播放 */
            break;
        default:
            break;
    }
}

/**
 * @brief 处理特殊指令
 */
static void coapi_cmd_type_special(int action, void *data)
{
    switch (action) {
        case CMD_WIRELESS_CTRL:
            break;
        case CMD_ALARM_UPDATE:  /* 更新闹钟,用户有通过语音设置、更新/删除闹钟 */
            coapi_timer_req(TIMER_LOCAL_CNT); /* 更新最近的三个闹钟到本地 ,此处会和底层冲突，造成云闹钟死机 */
            break;
		case CMD_CODE_BIND:
        default:
            break;
    }
}
/**
 * @brief AI相关的语音控制指令处理
 */
void coapi_cmd_type_ai(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_AI_VAD_ENTER:  /* 进入AI的vad连续对讲模式 */
            break;
        case CMD_AI_VAD_EXIT:  /* 退出AI的vad连续对讲模式 */
			break;
		default:
			break;
    }
}


/**
 * @brief 风扇控制相关的语音指令处理
 */
void coapi_cmd_type_fan(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);
	xdebug("=====fan： %d=======",cmd);
	
    switch (cmd) {
        case CMD_FAN_OPEN:  /* 打开风扇 */
            break;
        case CMD_FAN_CLOSE:  /* 关闭风扇 */
			break;
		case CMD_FAN_SPEED1:  /* 风速1挡 */
			break;
		case CMD_FAN_SPEED2:  /* 风速2挡 */
			break;
		case CMD_FAN_SPEED3:  /* 风速3挡 */
			break;
		default:
			break;
    }
}

/**
 * @brief 服务切换控制相关的语音指令处理
 */
void coapi_cmd_type_hostchange(int action)
{

}

/**
 * @brief 返回控制相关的语音指令处理
 */
void coapi_cmd_type_return(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_RETURN:  /* 返回 */
			xdebug("=====返回指令=======");
            break;
		default:
			break;
    }
}

/**
 * @brief 拍照控制相关的语音指令处理
 */
void coapi_cmd_type_take_photos(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_TAKE_PHOTO:  /* 拍照 */
			xdebug("=====拍照指令=======");
            break;
		default:
			break;
    }
}

/**
 * @brief 风扇控制相关的语音指令处理
 */
void coapi_cmd_type_video_chat(int action)
{
    int cmd = coapi_get_ctrl_cmd(action);

    switch (cmd) {
        case CMD_VIDEO_CHAT_START:  /* 进入视频聊天 */
			xdebug("=====进入视频聊天指令=======");
            break;
        case CMD_VIDEO_CHAT_STOP:  /* 退出视频聊天 */
			xdebug("=====退出视频聊天指令=======");
			break;
		default:
			break;
    }
}

/**
 * @brief 处理语音控制指令
 */
void coapi_cmd_ctrl_cb(int action, char *data)
{
    if (action == 0) {
        return;
    }
    int type = coapi_get_cmd_type(action);
    switch (type) {
        case CMD_TYPE_SWITCH:
            coapi_cmd_type_switch(action);
            break;
        case CMD_TYPE_VOLUME:
            xinfo("will do coapi_cmd_type_volume\n");
            coapi_cmd_type_volume(action);
            break;
        case CMD_TYPE_LEDCTRL:
            coapi_cmd_type_led(action);
            break;
        case CMD_TYPE_BATCHECK:
            coapi_cmd_type_battery(action);
            break;
        case CMD_TYPE_MOTION:
            coapi_cmd_type_motion(action);
            break;
        case CMD_TYPE_RING:
            coapi_cmd_type_ring(action);
            break;
        case CMD_TYPE_PLAYCTRL:
            xinfo("will do coapi_cmd_type_player\n");
            coapi_cmd_type_player(action);
            break;
        case CMD_TYPE_SPECIAL:
            coapi_cmd_type_special(action, data);
            break;
        case CMD_TYPE_AICTRL:
            coapi_cmd_type_ai(action);
            break;
        case CMD_TYPY_FANCTRL:
            coapi_cmd_type_fan(action);
            break;
        case CMD_TYPE_HOST_CHANGCTRL:
            coapi_cmd_type_hostchange(action);
            break;
        case CMD_TYPE_RETURNCTRL:
            coapi_cmd_type_return(action);
            break;
        case CMD_TYPE_TAKEPHOTOCTRL:
            coapi_cmd_type_take_photos(action);
            break;
        case CMD_TYPE_VIDEOCHATCTRL:
            coapi_cmd_type_video_chat(action);
            break;

        default:
            break;
    }

    return;
}
